package wsz.redisWork;

import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

import java.util.HashSet;
import java.util.Set;

/**
 * JedisCluster测试模拟
 * @author wsz
 * @date 2022/2/11 13:42
 **/
public class RunTest {

    JedisCluster jcd;

    @Before
    public void initRedis() {
        Set<HostAndPort> jedisClusterNode = new HashSet<>();
        jedisClusterNode.add(new HostAndPort("192.168.174.134", 7001));
        jedisClusterNode.add(new HostAndPort("192.168.174.134", 7002));
        jedisClusterNode.add(new HostAndPort("192.168.174.134", 7003));
        jedisClusterNode.add(new HostAndPort("192.168.174.134", 7004));
        jedisClusterNode.add(new HostAndPort("192.168.174.134", 7005));
        jedisClusterNode.add(new HostAndPort("192.168.174.134", 7006));
        jedisClusterNode.add(new HostAndPort("192.168.174.134", 7007));
        jedisClusterNode.add(new HostAndPort("192.168.174.134", 7008));

        jcd = new JedisCluster(jedisClusterNode);
    }

    @Test
    public void addKey() {
        String key = "test:001", value = "haha", newV= "hahaUpdate";

        // 新增key
        String result = jcd.set(key, value);
        System.out.println("set result=" + result);

        // 获取
        String getV1 = jcd.get(key);
        System.out.println("get getV1=" + getV1);

        // 修改
        jcd.set(key, newV);

        String getV2 = jcd.get(key);
        System.out.println("get getV2=" + getV2);

        // 删除
//        jcd.del(key);
    }
}
